﻿$(function () {    
    $('.editor').htmlarea({
        toolbarLoading: function (toolbar) {
            //// 'this' is equal to the jHtmlArea object
            //// 'toolbar' is equal to the jHtmlArea toolbar   
            // load extra toolbar buttons  
            var temp = editorToolbarItems.pop();
            while (temp){
                toolbar.push(temp);
                temp = editorToolbarItems.pop();
            } 
            return toolbar;
        },

        // Do something once the editor has finished loading
        loaded: function () {                        
//            var cName = 'jHtmlAreaCrrentHeight';
//            var value = 0;
//            var aCookie = document.cookie.split("; ");
//            for (var i = 0; i < aCookie.length; i++) {
//                var aCrumb = aCookie[i].split("=");
//                if (cName == aCrumb[0])
//                    value = aCrumb[1];
//            }
//            if (value != 0) {
//                this.iframe.height(parseInt(value));
//                this.textarea.height(parseInt(value));
//            }
            $('.jHtmlArea').width('100%');
            this.iframe.width('100%');
            this.textarea.width('100%');
            this.toolbar.width('100%');           
        },
        toolbar: [
            ["html"], ["bold", "italic", "underline", "strikeThrough"],
            ["orderedList", "unorderedList"],
            ["indent", "outdent"],
            ["justifyLeft", "justifyCenter", "justifyRight"],
            ["p", "h1", "h2", "h3", "h4", "h5", "h6"],
            ["link", "unlink"],
            [{
                // This is how to add a completely custom Toolbar Button
                css: "expand_editor",
                text: "Expand Editor",
                action: function (btn) {
                    if (this.textarea.height() == 500) {
                        var cNameUser = 'jHtmlAreaUseSetHeight';
                        var value = 200;
                        // cookies are separated by semicolons
                        var aCookie = document.cookie.split("; ");
                        for (var i = 0; i < aCookie.length; i++) {
                            // a name/value pair (a crumb) is separated by an equal sign
                            var aCrumb = aCookie[i].split("=");
                            if (cNameUser == aCrumb[0])
                                value = aCrumb[1];
                        }
                        this.iframe.height(parseInt(value));
                        this.textarea.height(parseInt(value));            
                        document.cookie = 'jHtmlAreaCrrentHeight=' + value + ';';
                    }
                    else {
                        //save user set height                            
                        document.cookie = 'jHtmlAreaUseSetHeight=' + this.iframe.height() + ';';
                        this.iframe.height(parseInt(500));
                        this.textarea.height(parseInt(500));
                        document.cookie = 'jHtmlAreaCrrentHeight=' + 500 + ';';
                    }
                }
            }]
            ,
            [{                
                css: "image_plugin",
                text: "Image",
                action: function (btn) {
                    media_library(this);
                }
            }]
        ]
    });
    });

// an array to hold custom editor toolbar buttons
var editorToolbarItems = [];
// call this method to add a custom toolbar button to the editor
function AddEditorToolbarItem(item) {
    editorToolbarItems.push(item);
}